//
//  SimButton.m
//
//  Created by LiuXubin on 15/3/13.
//

#import "SimButton.h"
#import "UIImage+SimAdditions.h"
#import "UIView+SimAdditions.h"
#import "SimDefine.h"

#define kSimButtonGap 5

@interface SimButton()
{
    CGPoint _beginPoint;
    
    BOOL _dragTriggerd;
}

@end

@implementation SimButton

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _minDragTriggerInterval = 0.2;
    }
    
    return self;
}

- (void)setOffset:(CGFloat)offset
{
    if (_offset != offset) {
        _offset = offset;
        if (_iconPostion == BIP_Left || _iconPostion == BIP_Right){
            [self setContentEdgeInsets:UIEdgeInsetsMake(0, _offset/2, 0, _offset/2)];
        }
        else if (_iconPostion == BIP_Top || _iconPostion == BIP_Bottom){
            [self setContentEdgeInsets:UIEdgeInsetsMake(_offset/2, 0, _offset/2, 0)];
        }
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    if (_iconPostion != BIP_None) {
        [self.titleLabel sizeToFit];
        if (_iconPostion == BIP_Left || _iconPostion == BIP_Right) {
            self.titleLabel.width = MIN(self.width-_offset-kSimButtonGap-self.imageView.width, self.titleLabel.width);
        }
        else{
            self.titleLabel.width = MIN(self.width-kSimButtonGap, self.titleLabel.width);
        }
        if (_iconPostion == BIP_Left){
            self.titleLabel.centerY = self.imageView.centerY = self.height/2;
            if (self.contentHorizontalAlignment == UIControlContentHorizontalAlignmentLeft) {
                self.imageView.left = 0;
                self.titleLabel.left = self.imageView.right+_offset;
            }
            else if  (self.contentHorizontalAlignment == UIControlContentHorizontalAlignmentRight) {
                self.titleLabel.right = self.width;
                self.imageView.right = self.titleLabel.left-_offset;
            }
            else{
                CGFloat width = self.imageView.width + self.titleLabel.width + _offset;
                self.titleLabel.right = self.width/2 + width/2;
                self.imageView.left = self.width/2 - width/2;
            }
        }
        else if (_iconPostion == BIP_Right) {
            self.titleLabel.centerY = self.imageView.centerY = self.height/2;
            if (self.contentHorizontalAlignment == UIControlContentHorizontalAlignmentLeft) {
                self.titleLabel.left = 0;
                self.imageView.left = self.titleLabel.right+_offset;
            }
            else if  (self.contentHorizontalAlignment == UIControlContentHorizontalAlignmentRight) {
                self.imageView.right = self.width;
                self.titleLabel.right = self.imageView.left-_offset;
            }
            else{
                CGFloat width = self.imageView.width + self.titleLabel.width + _offset;
                self.imageView.right = self.width/2 + width/2;
                self.titleLabel.left = self.width/2 - width/2;
            }
        }
        else if (_iconPostion == BIP_Center) {
            self.imageView.centerX = self.width/2;
            self.titleLabel.centerX = self.width/2;
        }
        else if (_iconPostion == BIP_Top){
            CGFloat height = self.imageView.height + self.titleLabel.height + _offset;
            self.imageView.top = self.height/2 - height/2;
            self.titleLabel.bottom = self.height/2 + height/2;
            self.imageView.centerX = self.width/2;
            self.titleLabel.centerX = self.width/2;
        }
        else if (_iconPostion == BIP_Bottom) {
            CGFloat height = self.imageView.height + self.titleLabel.height + _offset;
            self.imageView.bottom = self.height/2 + height/2;
            self.titleLabel.top = self.height/2 - height/2;
            self.imageView.centerX = self.width/2;
            self.titleLabel.centerX = self.width/2;
        }
    }
}

- (void)sizeToFit
{
    if (_iconPostion != BIP_None) {
        
        CGSize lbSize = SIM_MULTILINE_TEXTSIZE2(self.titleLabel.text, self.titleLabel.font, CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX));
        CGSize imageSize = self.imageView.image.size;
        CGFloat width = 0, height = 0;
        if (_iconPostion == BIP_Left || _iconPostion == BIP_Right){
            width = lbSize.width + imageSize.width + _offset + kSimButtonGap;
            height = MAX(lbSize.height, imageSize.height);
        }
        else if (_iconPostion == BIP_Top || _iconPostion == BIP_Bottom) {
            height = lbSize.height + imageSize.height + _offset;
            width = MAX(lbSize.width, imageSize.width) + kSimButtonGap;
        }
        else if (_iconPostion == BIP_Center) {
            width = MAX(lbSize.width, imageSize.width) + kSimButtonGap;
            height = MAX(lbSize.height, imageSize.height) + kSimButtonGap;
        }
        self.size = CGSizeMake(width, height);
    }
    else{
        [super sizeToFit];
    }
}

#pragma mark -
- (void)setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state
{
    UIImage *image = [UIImage imageWithColor:backgroundColor];
    [self setBackgroundImage:image forState:state];
}

#pragma mark - drag
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (_dragEnable) {
        UITouch *touch = [touches anyObject];
        _beginPoint = [touch locationInView:self];
        [self sendActionsForControlEvents:UIControlEventTouchDown];
        self.highlighted = YES;
    }
    else{
        [super touchesBegan:touches withEvent:event];
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (_dragEnable) {
        UITouch *touch = [touches anyObject];
        CGPoint nowPoint = [touch locationInView:self];
        float offsetX = nowPoint.x - _beginPoint.x;
        float offsetY = nowPoint.y - _beginPoint.y;
        if (!_dragTriggerd) {
            if (abs(offsetX) > 2 || abs(offsetY) > 2) {
                _dragTriggerd = YES;
                self.alpha = 0.6;
                self.highlighted = NO;
            }
        }
        
        if (_dragTriggerd) {
            if (_adsorbEnable) {
                self.center = CGPointMake(self.center.x, self.center.y + offsetY);
            }
            else{
                self.center = CGPointMake(self.center.x + offsetX, self.center.y + offsetY);
            }
        }
    }
    else{
        [super touchesMoved:touches withEvent:event];
    }
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (_dragEnable) {
        if (_dragTriggerd == YES) {
            self.alpha = 1.0;
            _dragTriggerd = NO;
            
            if (self.superview && _adsorbEnable ) {
                float marginLeft = self.frame.origin.x;
                float marginRight = self.superview.frame.size.width - self.frame.origin.x - self.frame.size.width;
                [UIView animateWithDuration:0.125 animations:^(void){
                    CGFloat topY = MIN(MAX(0, self.frame.origin.y),  self.superview.frame.size.height-self.frame.size.height);
                    self.frame = CGRectMake(marginLeft<marginRight?_adsordPadding:self.superview.frame.size.width - self.frame.size.width-_adsordPadding,
                                            topY,
                                            self.frame.size.width,
                                            self.frame.size.height);
                }];
            }
        }
        else{
            self.highlighted = NO;
            [self sendActionsForControlEvents:UIControlEventTouchUpInside];
        }
    }
    else{
        [super touchesEnded:touches withEvent:event];
    }
}

@end
